//
//  BrowserView.m
//  testBigImage
//
//  Created by 李小龙 on 2016/12/1.
//  Copyright © 2016年 bianwenhui. All rights reserved.
//

#import "BrowserView.h"

#define MaxScale 3.0
#define MinScale 0.5

@interface BrowserView ()

@property (nonatomic, assign) CGRect oriFrame;
@property (nonatomic, strong) UIView *oriImgView;

@property (nonatomic, weak) UIButton *resumeBtn;
@property (nonatomic, weak) UIImageView *showImgView;
@property (nonatomic, assign) CGRect showFrame;
@property (nonatomic, assign) CGFloat scale;

@end

@implementation BrowserView

+ (void)showImageView:(UIView *)imgView
{
    if ([imgView isKindOfClass:[UIImageView class]]) {
        if (((UIImageView *)imgView).image == nil) {
            return;
        }
    } else if ([imgView isKindOfClass:[UIButton class]]) {
        if (((UIButton *)imgView).currentImage == nil) {
            return;
        }
    } else {
        return;
    }
    
    BrowserView *view = [[BrowserView alloc] initWithImgView:imgView];
    view.frame = [UIScreen mainScreen].bounds;
    
    [[UIApplication sharedApplication].keyWindow addSubview:view];
    
    [UIView animateWithDuration:.5f animations:^{
        view.showImgView.frame = view.showFrame;
        view.alpha = 1;
    } completion:nil];
    
}

- (instancetype)initWithImgView:(UIView *)imgView
{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor blackColor];
        self.alpha = 0;
        
        [self setupUIWithImgView:imgView];
    }
    return self;
}

- (void)setupUIWithImgView:(UIView *)imgView
{
    self.oriImgView = imgView;
    self.oriFrame = [imgView convertRect:imgView.bounds
                                  toView:[UIApplication sharedApplication].keyWindow];
    
    CGSize size = [UIScreen mainScreen].bounds.size;
    
    // resumeBtn
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setBackgroundImage:[UIImage imageNamed:@"resume"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(resume) forControlEvents:UIControlEventAllEvents];
    btn.frame = CGRectMake(size.width - 32 - 15, size.height - 32 - 15, 32, 32);
    [self addSubview:btn];
    self.resumeBtn = btn;
    
    // showImgView
    UIImageView *showImgView = [[UIImageView alloc] initWithFrame:self.oriFrame];
    [showImgView setUserInteractionEnabled:YES];
    [showImgView setMultipleTouchEnabled:YES];
    
    UIImage *img;
    if ([imgView isKindOfClass:[UIImageView class]]) {
        img = ((UIImageView *)imgView).image;
    } else if ([imgView isKindOfClass:[UIButton class]]) {
        img = ((UIButton *)imgView).currentImage;
    }
    
    showImgView.image = img;
    [self addGestureRecognizerToView:showImgView];
    
    [self insertSubview:showImgView belowSubview:self.resumeBtn];
    self.showImgView = showImgView;
    
    
    CGFloat radio = img.size.width / img.size.height;
    CGFloat x = 0;
    CGFloat w = size.width;
    CGFloat h = w / radio;
    CGFloat y = (size.height - h) * 0.5;
    self.showFrame = CGRectMake(x, y, w, h);
    
    self.scale = 1.0;
}

- (void)resume
{
    [UIView animateWithDuration:.5f animations:^{
        self.showImgView.frame = self.showFrame;
        self.showImgView.transform = CGAffineTransformIdentity;
    }];
}

// 添加所有的手势
- (void)addGestureRecognizerToView:(UIView *)view
{
    // 旋转手势
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateView:)];
    [view addGestureRecognizer:rotation];
    
    // 缩放手势
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];
    [view addGestureRecognizer:pinch];
    
    // 移动手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];
    [view addGestureRecognizer:pan];
    
    // 点击事件
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss:)];
    [view addGestureRecognizer:tap];
}

// 处理旋转手势
- (void)rotateView:(UIRotationGestureRecognizer *)rotationGestureRecognizer
{
    UIView *view = rotationGestureRecognizer.view;
    if (rotationGestureRecognizer.state == UIGestureRecognizerStateBegan || rotationGestureRecognizer.state == UIGestureRecognizerStateChanged) {
        view.transform = CGAffineTransformRotate(view.transform, rotationGestureRecognizer.rotation);
        [rotationGestureRecognizer setRotation:0];
    }
}

// 处理缩放手势
- (void)pinchView:(UIPinchGestureRecognizer *)pinchGestureRecognizer
{
    UIView *view = pinchGestureRecognizer.view;
    
    CGFloat scale = pinchGestureRecognizer.scale;
    if ((scale > 1.0 && self.scale > MaxScale)
        || (scale < 1.0 && self.scale < MinScale)) {
        return;
    }
    
    view.transform = CGAffineTransformScale(view.transform, pinchGestureRecognizer.scale, pinchGestureRecognizer.scale);
    self.scale *= scale;
    pinchGestureRecognizer.scale = 1;
}

// 处理拖拉手势
- (void)panView:(UIPanGestureRecognizer *)panGestureRecognizer
{
    UIView *view = panGestureRecognizer.view;
    if (panGestureRecognizer.state == UIGestureRecognizerStateBegan || panGestureRecognizer.state == UIGestureRecognizerStateChanged) {
        CGPoint translation = [panGestureRecognizer translationInView:view.superview];
        [view setCenter:(CGPoint){view.center.x + translation.x, view.center.y + translation.y}];
        [panGestureRecognizer setTranslation:CGPointZero inView:view.superview];
    }
}


/** 点击 消失 */
- (void)dismiss:(UITapGestureRecognizer *)tap
{
    UIView *view = tap.view;
    [UIView animateWithDuration:.3f animations:^{
        view.frame = self.oriFrame;
        self.alpha = 0;
    } completion:^(BOOL finished) {
        self.oriFrame = CGRectNull;
        self.oriImgView = nil;
        self.showImgView = nil;
        
        [self removeFromSuperview];
    }];
}



@end

